package controller

import (
	"gadmin/model"
	"gadmin/service"
	"github.com/gin-gonic/gin"
	"strconv"
)

func GetTable(c *gin.Context) {
	tables, err := service.GetTable()
	if err != nil {
		SendError(c, err)
		return
	}
	SendData(c, tables)
}

func CreateTable(c *gin.Context) {
	var request model.Table
	err := c.BindJSON(&request)
	if err != nil {
		SendError(c, err)
		return
	}
	err = service.CreateTable(request)
	if err != nil {
		SendError(c, err)
		return
	}
	SendData(c, nil)
}

func DeleteTable(c *gin.Context) {
	id, err := strconv.ParseInt(c.Query("id"), 10, 32)
	if err != nil {
		SendError(c, err)
		return
	}
	err = service.DeleteTable(id)
	if err != nil {
		SendError(c, err)
		return
	}
	SendOK(c)
}

func CreateColumn(c *gin.Context) {
	var request model.Column
	err := c.BindJSON(&request)
	if err != nil {
		SendError(c, err)
		return
	}
	column, err := service.CreateColumn(request.TableID, request.Type, request.Title,
		request.DataIndex, request.Order)
	if err != nil {
		SendError(c, err)
		return
	}
	SendData(c, column)
}
